package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;



/**
 * 
 * @author zsj on 2008-4-29
 *
 */
public class StopFigure extends InteractionFragmentFigure {
	
	
	private static final Dimension SIZE = new Dimension(20, 20);

	
	private boolean canMove = true;
	
    protected void paintFigure(Graphics g) {
        Rectangle r = getBounds();
        g.setBackgroundColor(this.getBackgroundColor());
        g.setForegroundColor(this.getForegroundColor());
        g.drawLine(r.x, r.y, r.right(), r.bottom());
        g.drawLine(r.x, r.bottom(), r.right(), r.y);
    }

    /*
     * This method shouldn't be used, as the next method replaces it public Rectangle
     * getPreferredBounds(Rectangle suggestedBounds) { if (suggestedBounds.width == 0)
     * return new Rectangle(suggestedBounds.getLocation(), SIZE); else return
     * suggestedBounds; //return suggestedBounds; //throw new
     * UnsupportedOperationException("Not used"); }
     */

    /*
     * public Rectangle getPreferredModelBounds(final Rectangle bounds, final Rectangle
     * coveredBounds) { if (coveredBounds != null) return new Rectangle( coveredBounds.x +
     * (coveredBounds.width - bounds.width) / 2, coveredBounds.bottom() - bounds.height /
     * 2, SIZE.width, SIZE.height); else return bounds.getCopy(); }
     */

    @Override
    public Rectangle getPreferredModelBounds(final Rectangle bounds, final Rectangle coveredBounds) {
        if (coveredBounds != null) {
            int prefx = coveredBounds.x + coveredBounds.width / 2;
            int prefy = coveredBounds.bottom();
            return new Rectangle(prefx, prefy, 1, 1);
        } else
            return bounds.getCopy();
    }

    @Override
    public Rectangle getPreferredFigureBounds(final Rectangle bounds, final Rectangle coveredBounds) {
        Rectangle preferredLocation = super.getPreferredModelBounds(bounds, coveredBounds);
        return new Rectangle(preferredLocation.x - SIZE.width / 2, preferredLocation.y
                - SIZE.height, SIZE.width, SIZE.height);
    }

    // --- ILabeledFigure ---

    public Rectangle getLabelBounds() {
        return null; // not used
    }

    public void setLabel(String labelText) {
        // not used
    }
    
    public void setCanMove(boolean canMove) {
    	this.canMove = canMove;
    }
    
    @Override
    public void setBounds(Rectangle rect) {
    	// TODO Auto-generated method stub
    	if (this.canMove || this.getBounds().width <= 0 || this.bounds.height <= 0) {
    		super.setBounds(rect);
    	}
    	System.out.println(rect);
    }
}
